package net.oschina.git.fsppp.widget;

import net.oschina.git.fsppp.model.P3Dir;

import javax.swing.*;
import java.awt.*;
import java.util.List;

/**
 * Created by Qi_Huang on 2016/9/25.
 * 根据文件类型不同显示不同图标
 */
public class MyListCellRenderer extends DefaultListCellRenderer {
    private ImageIcon[] icons;

    public void onChange(List<P3Dir> dirs) {
        if (icons != null) {
            icons = null;
        }
        Class mClass = getClass();
        icons = new ImageIcon[dirs.size()];
        for (int i = 0, p = dirs.size(); i < p; i++) {
            int type = dirs.get(i).getType();
            String resource = (type == 0)? "/image/file.png" : "/image/dir.png";
            icons[i] = new ImageIcon(mClass.getResource(resource));
        }
    }

    @Override
    public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        setIcon(icons[index]);
        setText(((P3Dir)value).getName());
        setBackground(isSelected? list.getSelectionBackground() : list.getBackground());
        setForeground(isSelected? list.getSelectionForeground() : list.getForeground());
        return this;
    }
}
